load("@rules_cc//cc:defs.bzl", "cc_binary", "objc_library")
load("//bazel:flags.bzl", "CPP_FLAGS", "MAPLIBRE_FLAGS")

objc_library(
    name = "metal_backend",
    srcs = [
        "glfw_backend.hpp",
        "glfw_metal_backend.h",
        "glfw_metal_backend.mm",
        "metal_backend.h",
        "metal_backend.mm",
    ],
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    deps = [
        "//:maplibre_lib",
        "//vendor:args",
        "@glfw",
    ],
)

cc_binary(
    name = "glfw_app",
    srcs = glob(
               [
                   "*.cpp",
                   "*.hpp",
               ],
               exclude = [
                   "glfw_*_backend.*",
               ],
           ) +
           select({
               "//:metal_renderer": [
               ],
               "//conditions:default": [
                   "glfw_gl_backend.cpp",
                   "glfw_gl_backend.hpp",
               ],
           }),
    copts = CPP_FLAGS + MAPLIBRE_FLAGS,
    data = glob(["assets/*"]),
    defines = [
        r"MLN_ASSETS_PATH=\"assets/\"",
    ],
    linkopts = [
        "-lglfw",
    ] + select({
        "@platforms//os:macos": [
            "-framework Cocoa",
            "-framework IOKit",
        ],
        "//conditions:default": [],
    }),
    visibility = [
        "@rules_xcodeproj//xcodeproj:generated",
    ],
    deps = [
        "//:maplibre_lib",
        "@glfw",
        "@tinyobjloader",
    ] + select({
        "@platforms//os:macos": ["metal_backend"],
        "@platforms//os:linux": ["//platform/linux:impl"],
        "//conditions:default": [],
    }),
)
